home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / stringschemademo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-09  |  2.7 KB  |  89 lines

  1. {
  2. GPC demo program about strings being schemata (Extended Pascal feature).
  3. This is similar to schemademo.pas.
  4.  
  5. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  6.  
  7. Author: Frank Heckenbach <frank@pascal.gnu.de>
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, version 2.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22.  
  23. As a special exception, if you incorporate even large parts of the
  24. code of this demo program into another program with substantially
  25. different functionality, this does not cause the other program to
  26. be covered by the GNU General Public License. This exception does
  27. not however invalidate any other reasons why it might be covered
  28. by the GNU General Public License.
  29. }
  30.  
  31. program StringSchemaDemo;
  32.  
  33. {
  34.   A procedure taking a string (schema) parameter. A string is a built-in
  35.   schema type with the following declaration:
  36.  
  37.   String (Capacity : Cardinal) = record
  38.     Length : 0 .. Capacity;
  39.     Chars  : packed array [1 .. Capacity + 1] of Char
  40.   end;
  41. }
  42. procedure Test (var s : String);
  43. begin
  44.   Writeln ('The procedure was called with a string of capacity ', s.Capacity, ', length ', Length (s));
  45.   Writeln ('and the value ''', s, '''.');
  46.   Writeln;
  47.   Writeln ('Now we append ''bar'' to the string. However, the routines know the');
  48.   Writeln ('capacity of the string, so if appending to it would make it longer than');
  49.   Writeln ('its capacity, they will not write beyond the capacity. This works simply');
  50.   Writeln ('because strings are schemata, without any work required by the programmer.');
  51.   Writeln;
  52.   s := s + 'bar';
  53.   Writeln ('The string has now a capacity of ', s.Capacity, ' (still), a length of ', Length (s));
  54.   Writeln ('and the value ''', s, '''.');
  55.   Writeln
  56. end;
  57.  
  58. const
  59.   Foo : String (10) = 'foo';
  60.  
  61. var
  62.   a : Integer;
  63.   StrPtr : ^String;
  64.  
  65. begin
  66.   Test (Foo);
  67.  
  68.   repeat
  69.     Write ('Enter the capacity for a string: ');
  70.     Readln (a)
  71.   until a >= 1;
  72.  
  73.   { A schema array declared in the statement part (GPC extension) }
  74.   var t : String (a);
  75.  
  76.   Write ('Enter the contents of the string: ');
  77.   Readln (t);
  78.   Writeln;
  79.   Test (t);
  80.  
  81.   Writeln ('Now we dynamically allocate a string of the same capacity.');
  82.   New (StrPtr, a);
  83.   Write ('Enter the contents of the new string: ');
  84.   Readln (StrPtr^);
  85.   Writeln;
  86.   Test (StrPtr^);
  87.   Dispose (StrPtr)
  88. end.
  89.